from lxml import etree
import requests
from Logs.log import log1
from db_sql import insert_db
from utils import copper_url as url, headers, cop
from datetime import datetime
from retrying import retry


@retry(stop_max_attempt_number=3)
def request():
    resp = requests.get(url, headers=headers)
    html = etree.HTML(resp.content.decode())
    return html


def parse(html):
    db_dict = {}
    main_label = html.xpath("//table[@class='content2']//td[contains(text(), '1#铜')]/..")[0]
    db_dict["raw_name"] = main_label.xpath("./td[2]/text()")[0].strip()
    db_dict["avg_price"] = main_label.xpath("./td[5]/text()")[0].replace(",", "").strip()
    db_dict["lower_price"] = main_label.xpath("./td[3]/text()")[0].replace(",", "").strip()
    db_dict["higher_price"] = main_label.xpath("./td[4]/text()")[0].replace(",", "").strip()
    db_dict["change_amount"] = main_label.xpath("./td[6]/text()")[0].strip()
    db_dict["change_range"] = ""
    db_dict["unit"] = "元/吨"
    price_date = main_label.xpath("./td[7]/text()")[0].strip()
    price_date = datetime.strptime(price_date, '%Y-%m-%d')
    db_dict["price_date"] = price_date.strftime('%Y/%m/%d')
    timestamp = price_date.strftime('%Y%m%d')
    db_dict["id"] = timestamp + cop
    return db_dict


def save(db_dict):
    db_dict["raw_type"] = cop
    db_dict["source"] = "长江现货"
    db_dict["create_time"] = datetime.now().strftime('%Y/%m/%d %H:%M:%S')
    insert_db(db_dict)


def run():
    try:
        html = request()
        db_dict = parse(html)
        save(db_dict)
    except Exception as e:
        log1.error(e)
    log1.info("铜END!")


if __name__ == '__main__':
    run()